var router = require('koa-router')();



/** banner start **/
router.post('/banner/list',async (ctx, next)=>{
    let {
        page=1,
        size=10,
        title='',
        type='',
        status=''
    } = ctx.request.body;
    let count = await ctx.post('/admin/countBanner',{title,type,status});
    let result = await ctx.post('/admin/listBanner',{page,size,title,type,status});
    if(count.cd==0){
        result.total = count.data;
    }
    ctx.body = result;
});
//get
router.post('/banner/get', async (ctx,next) => {
    let {id} = ctx.request.body;
    let result = await ctx.post('/admin/getBannerById',{id});
    if(result.cd==0){
        delete result.data.create_id;
        delete result.data.created_at;
        delete result.data.update_id;
        delete result.data.updated_at;
    }
    ctx.body = result;
})
//查询banner排序
router.post('/banner/getPosition' , async (ctx , next) => {
    let {type} = ctx.request.body;
    ctx.body = await ctx.post('/admin/generatePosition' , {type});
})
//保存
router.post('/banner/save', async (ctx,next) => {
    let {banner} = ctx.request.body;
    banner.type = parseInt(banner.type);
    banner.status = parseInt(banner.status);
    banner.position = parseInt(banner.position)
    let result = null;
    if(banner.id){
        result = await ctx.post('/admin/updateBanner',{...banner})
    }else{
       result = await ctx.post('/admin/insertBanner',{...banner})
    }
    ctx.body = result;
})
//删除
router.post('/banner/delete' , async (ctx,next) => {
    let {id} = ctx.request.body;
    ctx.body = await ctx.post('/admin/deleteBannerById',{id});
})

//修改状态
router.post('/banner/status', async (ctx,next) => {
    let {id,status=2} = ctx.request.body;
    status = (status == 'true'?1:2);
    ctx.body = await ctx.post('/admin/updateBannerStatus',{id,status})
})
/** banner end **/

/** 文章管理 start **/
router.post('/article/list',async (ctx, next)=>{
    let {
        page=1,
        size=10,
        title='',
        status=''
    } = ctx.request.body;
    let count = await ctx.post('/admin/countArticle',{title,status});
    let result = await ctx.post('/admin/listArticle',{page,size,title,status});
    if(count.cd==0){
        result.total = count.data;
    }
    ctx.body = result;
});
//get
router.post('/article/get', async (ctx,next) => {
    let {id} = ctx.request.body;
    let result = await ctx.post('/admin/getArticleById',{id});
    if(result.cd==0){
        delete result.data.create_id;
        delete result.data.created_at;
        delete result.data.update_id;
        delete result.data.updated_at;
    }
    ctx.body = result;
})
//保存
router.post('/article/save', async (ctx,next) => {
    let {article} = ctx.request.body;
    let result = null;
    console.log(article,"========");
    if(article.id){
        result = await ctx.post('/admin/updateArticle',{...article})
    }else{
       result = await ctx.post('/admin/insertArticle',{...article})
    }
    ctx.body = result;
})
//删除
router.post('/article/delete' , async (ctx,next) => {
    let {id} = ctx.request.body;
    ctx.body = await ctx.post('/admin/deleteArticleById',{id});
})

//修改状态
router.post('/article/status', async (ctx,next) => {
    let {id,status=2} = ctx.request.body;
    status = (status == 'true'?1:2);
    ctx.body = await ctx.post('/admin/updateArticleStatus',{id,status})
})
/** 文章管理 end **/


/** 帮助文章管理 start **/
router.post('/helper/list',async (ctx, next)=>{
    let {
        page=1,
        size=10,
        title='',
        status=''
    } = ctx.request.body;
    let count = await ctx.post('/admin/countHelpDoc',{title,status});
    let result = await ctx.post('/admin/listHelpDoc',{page,size,title,status});
    if(count.cd==0){
        result.total = count.data;
    }
    ctx.body = result;
});
//get
router.post('/helper/get', async (ctx,next) => {
    let {id} = ctx.request.body;
    let result = await ctx.post('/admin/getHelpDocById',{id});
    if(result.cd==0){
        delete result.data.create_id;
        delete result.data.created_at;
        delete result.data.update_id;
        delete result.data.updated_at;
    }
    ctx.body = result;
})
//保存
router.post('/helper/save', async (ctx,next) => {
    let {article} = ctx.request.body;
    let result = null;
    if(article.id){
        result = await ctx.post('/admin/updateHelpDoc',{...article})
    }else{
       result = await ctx.post('/admin/insertHelpDoc',{...article})
    }
    ctx.body = result;
})
//删除
router.post('/helper/delete' , async (ctx,next) => {
    let {id} = ctx.request.body;
    ctx.body = await ctx.post('/admin/deleteHelpDocById',{id});
})
//修改状态
router.post('/helper/status', async (ctx,next) => {
    let {id,status=2} = ctx.request.body;
    status = (status == 'true'?1:2);
    ctx.body = await ctx.post('/admin/updateHelpDocStatus',{id,status})
})
/** 帮助文章管理 end **/

/** 短信管理 start**/
router.post('/sms/list' , async (ctx,next) => {
    let {page=1,size=10} = ctx.request.body;
    let count = await ctx.post('/admin/countMessage');
    let result = await ctx.post('/admin/listAllMessage',{page,size});
    if(count.cd==0 && result.cd == 0){
        result.total = count.data;
    }
    ctx.body = result;
})
//发送短信
router.post('/sms/send' , async (ctx,next) => {
    let {receiverStr,content,type} = ctx.request.body; 
    ctx.body = await ctx.post('/admin/sendMessage',{receiverStr,content,type});
})
//手工确认
router.post('/sms/confirm' , async (ctx,next) => {
    let {id} = ctx.request.body;
    ctx.body = await ctx.post('/admin/modifyMsgStatus',{id});
})
/** 短信管理 end**/


/** 客服管理 start**/
router.post('/kefu/list' , async (ctx,next) => {
    let {page=1,size=10} = ctx.request.body;
    let count = await ctx.post('/admin/countCustomerService');
    let result = await ctx.post('/admin/listCustomerService',{page,size});
    if(count.cd == 0 && result.cd == 0){
        result.total = count.data;
    }
    ctx.body = result;
})
//查看问题信息
router.post('/kefu/get' , async (ctx,next) => {
    let {id} = ctx.request.body;
    ctx.body = await ctx.post('/admin/findCustomerServiceById',{id});
})
//查看问题处置结果
router.post('/kefu/handleResult' , async (ctx,next) => {
    let {id} = ctx.request.body;
    ctx.body = await ctx.post('/admin/findCustomerServiceByIdFinished' , {id})
})
//处理问题
router.post('/kefu/handleIssue' , async (ctx,next) =>{
    let {id,solution,type} = ctx.request.body ;
    ctx.body = await ctx.post('/admin/updateCustomerService' , {id,solution,type})
})
/** 客服管理 end**/

export default router;
